package keye.document_manage.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 显示不规则窗体
 */
public class IrregularFormSample extends JFrame {

	private static final long serialVersionUID = 1L;
	private Point origin; //用于移动窗体
	private Image img; //用来设定窗体不规则样式的图片

	public IrregularFormSample() {
		this("./src/main/resources/picture/startIoc.png");
	}
	public IrregularFormSample(String imgUrl) {
		init(imgUrl);
	}
	
	public void init(String imgUrl){
		/* 首先初始化一张图片，我们可以选择一张有透明部分的不规则图片
		 * (当然我们要选择支持Alpha(透明)层的图片格式，如PNG)，这张
		 * 图片将被用来生成与其形状相同的不规则窗体
		 */
		MediaTracker mt=new MediaTracker(this);

		img = new ImageIcon(imgUrl).getImage();
		mt.addImage(img, 0);
		try {
			mt.waitForAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		try {
			initialize();//窗体初始化
		} catch (IOException e) {
			e.printStackTrace();
		}    
	}

	/**
	 * 窗体初始化
	 * @throws IOException
	 */
	private void initialize() throws IOException {
		//设定窗体大小和图片一样大
		this.setSize(img.getWidth(null), img.getHeight(null));
		//设定禁用窗体装饰，这样就取消了默认的窗体结构
		this.setUndecorated(true);
		//初始化用于移动窗体的原点
		this.origin=new Point();
		this.setBackground(new Color(0,0,0,0));
		this.setLocationRelativeTo(null);

		JPanel jp = new JPanel(new BorderLayout());
		jp.setOpaque(false);
		jp.add(new JLabel(new ImageIcon(img)),BorderLayout.CENTER);
		this.setContentPane(jp);
		
		//由于取消了默认的窗体结构，所以我们要手动设置一下移动窗体的方法
		addMouseListener( 
				new MouseAdapter(){
					public void mousePressed(MouseEvent e){
						origin.x = e.getX();
						origin.y = e.getY();
					}
					public void mouseReleased(MouseEvent e) {
						super.mouseReleased(e);
					}
					@Override
					public void mouseEntered(MouseEvent e) {
						repaint();              
					}            
				}
				);

		addMouseMotionListener(
				new MouseMotionAdapter(){
					public void mouseDragged(MouseEvent e){
						Point p =    getLocation();
						setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y );
					}          
				}
				);    
	}

	public static void main(String[] args) {
		IrregularFormSample sample = new IrregularFormSample();
		sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		sample.setLayout(null);
		JLabel jl = new JLabel("三国杀的结构二号路士大夫");
//		jl.setBounds(sample.getWidth()/2, sample.getHeight()/2, 50, 20);
		sample.getContentPane().add(jl,BorderLayout.SOUTH);
//		sample.getContentPane().set
		//GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		//通过调用GraphicsEnvironment的getDefaultScreenDevice方法获得当前的屏幕设备了
		//GraphicsDevice gd = ge.getDefaultScreenDevice();
		// 全屏设置
		//gd.setFullScreenWindow(null);
		sample.setVisible(true);
	}  
}